/**
 * 工单预警
 */
angular.module('inspinia',['infinity.angular-chosen']).controller('workWarningQueryCtrl',function($scope,$http,$state,i18nService,$document,$timeout){
    //数据源
    i18nService.setCurrentLang('zh-cn');
    $scope.paginationOptions = {pageNo : 1,pageSize : 10};
    $scope.agentLevel = $scope.$root.entityAgentLevel;

    $scope.containSelect = [{text:"是",value:'1'},{text:"否",value:'2'},{text:"仅直属下级",value:'3'}];
    $scope.workProductTypeSelect = [{text:"首刷投诉预警",value:'1'}];
    $scope.workProductTypeStr=angular.toJson($scope.workProductTypeSelect);

    $scope.resetForm = function () {
        $scope.info = {contain:"1"};
    }
    $scope.resetForm();

    //代理商动态控件
    $scope.changeAgent =changeAgent;
    var oldValueAgent="";
    var timeoutAgent="";
    function changeAgent(value) {
        var newValue=value;
        if(newValue != oldValueAgent){
            if (timeoutAgent) $timeout.cancel(timeoutAgent);
            timeoutAgent = $timeout(
              function(){
                  $scope.getMerchantList(value);
              },800);
        }
    }
    //代理商
    $scope.agentList=[{value:"",text:"全部"}];

    $http.post("agentInfo/selectAllInfo")
      .success(function(msg){
          //响应成功
          for(var i=0; i<msg.length; i++){
              $scope.agentList.push({value:msg[i].agentNo,text:msg[i].agentNo + " " + msg[i].agentName});
          }
      });

    //异步获取直属代理商
    var oldValue="";
    var timeout="";
    $scope.getAgentList = function(value) {
        $scope.agentt = [];
        var newValue=value;
        if(newValue != oldValue){
            if (timeout) $timeout.cancel(timeout);
            timeout = $timeout(
              function(){
                  $http.post('agentInfo/selectAllInfo','item=' + value,
                    {headers: {'Content-Type': 'application/x-www-form-urlencoded'}})
                    .then(function (response) {
                        if(response.data.length==0) {
                            $scope.agentt.push({value: "", text: "全部"});
                        }else{
                            $scope.agentt.push({value: "", text: "全部"});
                            for(var i=0; i<response.data.length; i++){
                                $scope.agentt.push({value:response.data[i].agentNo,text:response.data[i].agentNo + " " + response.data[i].agentName});
                            }
                        }
                        $scope.agentList = $scope.agentt;
                        oldValue = value;
                    });
              },800);
        }
    };



    $scope.columnDefs = [
        {field: 'id',displayName: 'ID',width: 120,pinnable: false,sortable: false},
        {field: 'workProductType',displayName:'预警类型',width:180,cellFilter:"formatDropping:" +  $scope.workProductTypeStr },
        {field: 'agentName',displayName: '接收预警代理商名称',width: 180,pinnable: false,sortable: false},
        {field: 'agentNo',displayName: '接收预警代理商编号',width: 180,pinnable: false,sortable: false},
        {field: 'agentLevel',displayName: '代理商等级',width: 300,pinnable: false,sortable: false},
        {field: 'warningTitle',displayName: '预警标题',width: 300,pinnable: false,sortable: false},
        {field: 'warningContent',displayName: '预警内容',width: 300,pinnable: false,sortable: false},
        {field: 'createTime',displayName: '预警时间',width: 220,pinnable: false,sortable: false,cellFilter:'date:"yyyy-MM-dd HH:mm:ss"'}
    ];
    //设置列表行显隐
    $scope.rowHideOrShow=function(list,key,checked){
        if(list!=null&&list.length>0){
            for(var i=0;i<list.length;i++){
                var item=list[i];
                if(key==item.field){
                    item.visible=checked;
                    break;
                }
            }
        }
    }

    $scope.myGrid = {
        paginationPageSize:10,                  //分页数量
        paginationPageSizes: [10,20,50,100],	//切换每页记录数
        useExternalPagination: true,		  //开启拓展名
        enableHorizontalScrollbar: true,        //横向滚动条
        enableVerticalScrollbar : true,  		//纵向滚动条
        columnDefs: $scope.columnDefs,
        onRegisterApi: function(gridApi) {
            $scope.gridApi = gridApi;
            $scope.gridApi.pagination.on.paginationChanged($scope, function (newPage, pageSize) {
                $scope.paginationOptions.pageNo = newPage;
                $scope.paginationOptions.pageSize = pageSize;
                $scope.query();
            });
        }
    };
    $scope.loadImg = false;
    $scope.query = function () {
        if ($scope.loadImg) {
            return;
        }
        $scope.loadImg = true;
        var data={
            info:angular.toJson($scope.info),
            pageNo:$scope.paginationOptions.pageNo,
            pageSize:$scope.paginationOptions.pageSize
        };
        var postCfg = {
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
            transformRequest: function (data) {
                return $.param(data);
            }
        };
        $http.post("workWarning/selectWorkWarningList",data,postCfg)
          .success(function(data){
              if(data.status){
                  $scope.myGrid.data=data.page.result;
                  $scope.myGrid.totalItems = data.page.totalCount;
              }else{
                  $scope.notice(data.msg);
              }
              $scope.loadImg = false;
          })
          .error(function(data){
              $scope.notice(data.msg);
              $scope.loadImg = false;
          });
    };

    //页面绑定回车事件
    $document.bind("keypress", function(event) {
        $scope.$apply(function (){
            if(event.keyCode == 13){
                $scope.query();
            }
        })
    });
});